package com.dome.thread2;

 class FirstThread implements Runnable {
     private Lock lock;
     public FirstThread(Lock lock) {
         this.lock=lock;
     }

     @Override
    public void run() {
            synchronized (lock){
                for (int i = 1; i <= 1000; i++) {
                    if (i%(2*3*5*7)==0){
                        try {
                            System.out.println(i+"进入等到模式");
                            //打开唤醒标记,将标记设定为true
                            lock.fatl=true;
                            //自己陷入等待
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                        //唤醒另外一条线程
                        lock.notify();
                    }
                }
            }
        }


class SecondThread implements Runnable {
    private Lock lock;
    public SecondThread(Lock lock) {
        this.lock = lock;
    }

    public int count = 0;

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                //判断是否唤醒
                if (lock.fatl) {
                    //唤醒,个数加1,将标记改为false,自己陷入等待
                    lock.notify();
                    System.out.println("唤醒:" + (++count) + "次");
                    lock.fatl = false;
                }else {
                    break;
                }
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

    class Lock {
        //唤醒标记
        boolean fatl = false;
    }

    class RunnableTest {
        public static void main(String[] args) {
            Lock lock = new Lock();
            FirstThread ft = new FirstThread(lock);
            SecondThread st = new SecondThread(lock);
            Thread t1 = new Thread(ft);
            Thread t2 = new Thread(st);
            t1.start();
            t2.start();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("个数为:"+st.count);
        }
    }

